<template>
  <el-table :data="tableData" border style="width: 100%">
    <el-table-column type="selection" width="55"></el-table-column>
    <template v-for="item in tableList">
      <el-table-column
        :prop="item.prop"
        :label="item.label"
        :width="item.width"
      ></el-table-column>
    </template>

    <el-table-column label="创建时间">
      <template #default="scope">
        {{ dateFilter(scope.row.createAt) }}
      </template>
    </el-table-column>

    <el-table-column label="修改时间">
      <template #default="scope">
        {{ dateFilter(scope.row.updateAt) }}
      </template>
    </el-table-column>

    <el-table-column label="状态" width="120">
      <template #default="scope">
        <el-switch
          v-model="scope.row.state"
          :active-value="1"
          :inactive-value="2"
          @click="switchChange(scope.row)"
        />
      </template>
    </el-table-column>

    <el-table-column label="功能" width="260">
      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.row)">
          编辑
        </el-button>
        <el-button size="small" type="danger" @click="handleDelete(scope.row)"
          >删除</el-button
        >
      </template>
    </el-table-column>
  </el-table>
</template>

<script setup>
import { dateFilter } from '@/utils/time'
import { ElMessage, ElMessageBox } from 'element-plus'
const value1 = ref()
const props = defineProps(['tableData', 'tableList', 'total'])
const emits = defineEmits([
  'handleEditEmit',
  'handleDeleteEmit',
  'switchChangeEmit'
])
const handleEdit = (page) => {
  emits('handleEditEmit', page.id)
}

const handleDelete = (page) => {
  let data = {
    id: page.id,
    status: 3
  }
  ElMessageBox.confirm('删除的数据不能恢复！确认删除吗？', '警告', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      emits('switchChangeEmit', data)
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除'
      })
    })
}

const switchChange = (val) => {
  let data = {
    id: val.id,
    status: val.state
  }
  emits('switchChangeEmit', data)
}
</script>

<style lang="scss" scoped>
.edit {
  padding: 20px 0;
}
.form-pagination {
  text-align: center;
  padding: 20px 0;
}
</style>
